home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / src / forms_timer.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-07  |  3.9 KB  |  155 lines

  1. //
  2. // "$Id: forms_timer.cxx,v 1.4 1999/01/07 19:17:45 mike Exp $"
  3. //
  4. // Forms timer object for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-1999 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  24. //
  25.  
  26. // Emulate the Forms Timer object
  27. // You don't want to use this if you just want a timeout, call
  28. // Fl::add_timeout directly!
  29.  
  30. #include <FL/Fl.H>
  31. #include <FL/Fl_Timer.H>
  32. #include <FL/fl_draw.H>
  33. #ifdef WIN32
  34. # include <sys/types.h> 
  35. # include <sys/timeb.h>
  36. #else
  37. # include <sys/time.h>
  38. #endif
  39. #include <stdio.h>
  40.  
  41. #define FL_TIMER_BLINKRATE    0.2
  42.  
  43. void fl_gettime(long* sec, long* usec) {
  44. #ifdef WIN32
  45.   struct timeb tp;
  46.   ftime(&tp);
  47.   *sec = tp.time;
  48.   *usec = tp.millitm * 1000;
  49. #else
  50.   struct timeval tp;
  51.   struct timezone tzp;
  52.   gettimeofday(&tp, &tzp);
  53.   *sec = tp.tv_sec;
  54.   *usec = tp.tv_usec;
  55. #endif
  56. }
  57.  
  58. void Fl_Timer::draw() {
  59.   int tt;
  60.   Fl_Color col;
  61.   char str[32];
  62.   if (!on || delay>0.0)
  63.     col = color();
  64.   else if ((int) (delay / FL_TIMER_BLINKRATE) % 2)
  65.     col = color();
  66.   else
  67.     col = selection_color();
  68.   draw_box(box(), col);
  69.   if (type() == FL_VALUE_TIMER && delay>0.0) {
  70.     double d = direction_ ? total-delay : delay;
  71.     if (d < 60.0)
  72.       sprintf(str, "%.1f", d);
  73.     else {
  74.       tt = (int) ((d+0.05) / 60.0);
  75.       sprintf(str, "%d:%04.1f", tt, d - 60.0 * tt);
  76.     }
  77.     fl_font(labelfont(), labelsize());
  78.     fl_color(labelcolor());
  79.     fl_draw(str, x(), y(), w(), h(), FL_ALIGN_CENTER);
  80.   } else
  81.     draw_label();
  82. }
  83.  
  84. void Fl_Timer::stepcb(void* v) {
  85.   ((Fl_Timer*)v)->step();
  86. }
  87.  
  88. void Fl_Timer::step() {
  89.   if (!on) return;
  90.   double lastdelay = delay;
  91.   long sec, usec; fl_gettime(&sec, &usec);
  92.   delay -= (double) (sec - lastsec) + (double) (usec - lastusec) / 1000000.0;
  93.   lastsec = sec; lastusec = usec;
  94.   if (lastdelay > 0.0 && delay <= 0.0) {
  95.     if (type() == FL_HIDDEN_TIMER) {
  96.       on = 0;
  97.       delay = 0;
  98.     } else {
  99.       redraw();
  100.       Fl::add_timeout(FL_TIMER_BLINKRATE, stepcb, this);
  101.     }
  102.     do_callback();
  103.   } else {
  104.     if (type() == FL_VALUE_TIMER) redraw();
  105.     Fl::add_timeout(FL_TIMER_BLINKRATE, stepcb, this);
  106.   }
  107. }
  108.  
  109. int Fl_Timer::handle(int event) {
  110.   if (event == FL_RELEASE && delay <= 0) value(0.0);
  111.   return 0;
  112. }
  113.  
  114. Fl_Timer::~Fl_Timer() {
  115.   Fl::remove_timeout(stepcb, this);
  116. }
  117.  
  118. Fl_Timer::Fl_Timer(uchar t, int x, int y, int w, int h, const char* l)
  119. : Fl_Widget(x, y, w, h, l) {
  120.   box(FL_DOWN_BOX);
  121.   selection_color(FL_RED);
  122.   delay = 0;
  123.   on = 0;
  124.   direction_ = 0;
  125.   type(t);
  126.   if (t == FL_HIDDEN_TIMER) clear_visible();
  127.   if (t == FL_VALUE_TIMER) align(FL_ALIGN_LEFT);
  128. }
  129.  
  130. void Fl_Timer::value(double d) {
  131.   delay = total = d;
  132.   on = (d > 0.0);
  133.   fl_gettime(&(lastsec), &(lastusec));
  134.   if (type() != FL_HIDDEN_TIMER) redraw();
  135.   Fl::remove_timeout(stepcb, this);
  136.   if (on) Fl::add_timeout(FL_TIMER_BLINKRATE, stepcb, this);
  137. }
  138.  
  139. void Fl_Timer::suspended(char d) {
  140.   if (!d) {
  141.     if (on) return;
  142.     on = (delay > 0.0);
  143.     fl_gettime(&(lastsec), &(lastusec));
  144.     if (on) Fl::add_timeout(FL_TIMER_BLINKRATE, stepcb, this);
  145.   } else {
  146.     if (!on) return;
  147.     on = 0;
  148.     Fl::remove_timeout(stepcb, this);
  149.   }
  150. }
  151.  
  152. //
  153. // End of "$Id: forms_timer.cxx,v 1.4 1999/01/07 19:17:45 mike Exp $".
  154. //
  155.